<?php
require_once("DataBase.inc");

function remove_where($list, $field) {
    if ($list === false) return "";
    $w = "";
    foreach ($list as $a) {
        if ($w<>"") $w .= " OR ";
        $w .= $field."=".$a[$field];		
    }
    return $w;
}

if (isset($_POST["remove_campaign_id"])) {
    $c = DataBase::select("selection_campaign", null, array("selection_campaign_id"=>$_POST["remove_campaign_id"]));
    echo "You are going to remove the selection campaign ".$c[0]["selection_campaign_name"]."<br/>";
    echo "This will also remove all applicants, written exams, interviews... associated to this campaign.<br/>";
    echo "Are you sure you want to do that ?<br/>";
    echo "<br/><a href='?remove_campaign_id=".$_POST["remove_campaign_id"]."'>Yes I'm sure</a><br/>";
    echo "<br/><a href='?'>No, go back to select a campaign</a><br/>";
    die();
} else if (isset($_GET["remove_campaign_id"])) {
    $applicants = DataBase::select("applicant",array("applicant_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    $w = remove_where($applicants, "applicant_id");
    if ($w <> "") {
        DataBase::query("DELETE FROM applicant WHERE ".$w);
        DataBase::query("DELETE FROM applicant_interest WHERE ".$w);
        DataBase::query("DELETE FROM applicant_exam_room_beta WHERE ".$w);
        DataBase::query("DELETE FROM applicant_more_info WHERE ".$w);
        DataBase::query("DELETE FROM applicant_score_part_exam WHERE ".$w);
        DataBase::query("DELETE FROM applicant_score_subject_exam WHERE ".$w);
        DataBase::query("DELETE FROM applicant_eligibility WHERE ".$w);
        DataBase::query("DELETE FROM applicant_interview WHERE ".$w);
    }
    DataBase::query("DELETE FROM selection_campaign WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $list = DataBase::select("exam_center",array("exam_center_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM exam_center WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "exam_center_id");
    if ($w <> "") DataBase::query("DELETE FROM exam_center_rooms WHERE ".$w);
    $list = DataBase::select("exam",array("exam_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM exam WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "exam_id");
    if ($w <> "") DataBase::query("DELETE FROM supervisor WHERE ".$w);
    DataBase::query("DELETE FROM selection_campaign_programs WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $list = DataBase::select("subject",array("subject_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM subject WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "subject_id");
    if ($w <> "") DataBase::query("DELETE FROM subject_part WHERE ".$w);
    $list = DataBase::select("exam_copie",array("exam_copie_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM exam_copie WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "exam_copie_id");
    if ($w <> "") DataBase::query("DELETE FROM exam_copie_content WHERE ".$w);
    $list = DataBase::select("exam_eligibility_rule",array("exam_eligibility_rule_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM exam_eligibility_rule WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "exam_eligibility_rule_id");
    if ($w <> "") DataBase::query("DELETE FROM exam_eligibility_rule_subjects WHERE ".$w);
    $list = DataBase::select("interview_center",array("interview_center_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM interview_center WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "interview_center_id");
    if ($w <> "") DataBase::query("DELETE FROM interview_center_rooms WHERE ".$w);
    $list = DataBase::select("interview",array("interview_id"),array("selection_campaign_id"=>$_GET["remove_campaign_id"]));
    DataBase::query("DELETE FROM interview WHERE selection_campaign_id=".$_GET["remove_campaign_id"]);
    $w = remove_where($list, "interview_id");
    if ($w <> "") DataBase::query("DELETE FROM interviewer WHERE ".$w);
    if ($w <> "") DataBase::query("DELETE FROM interview_observer WHERE ".$w);
    echo "Campaign removed.<br/><br/>";
}
?>

<form name='campaign' method='post'>
    Select a campaign: <select name='campaign_id'>
        <?php
        $list = DataBase::select("selection_campaign", null, null);
        foreach ($list as $r) {
            $programs = DataBase::query("SELECT * FROM selection_campaign_programs LEFT JOIN program ON program.program_id=selection_campaign_programs.program_id WHERE selection_campaign_programs.selection_campaign_id=".$r["selection_campaign_id"]);
            $s = "";
            foreach ($programs as $p) {
                if (strlen($s)>0) $s.=" + ";
                $s .= $p["program_name"];
            }
            $s = $r["selection_campaign_name"]." (".$s.")";
            echo "<option value='".$r["selection_campaign_id"]."'>".$s."</option>";
        }
        ?>
    </select>
    <button type='submit'>Ok</button><br/>
    Or <a href="create_campaign.php">create a new one</a>
</form>
<br/><br/>
You can also remove a campaign: 
<form name='campaign' method='post'>
    <select name='remove_campaign_id'>
        <?php
        foreach ($list as $r) {
            $programs = DataBase::query("SELECT * FROM selection_campaign_programs LEFT JOIN program ON program.program_id=selection_campaign_programs.program_id WHERE selection_campaign_programs.selection_campaign_id=".$r["selection_campaign_id"]);
            $s = "";
            foreach ($programs as $p) {
                if (strlen($s)>0) $s.=" + ";
                $s .= $p["program_name"];
            }
            $s = $r["selection_campaign_name"]." (".$s.")";
            echo "<option value='".$r["selection_campaign_id"]."'>".$s."</option>";
        }
        ?>
    </select>
    <button type='submit'>Remove</button><br/>
</form>
